<?php
namespace Admin\Controller;
use Think\Controller;
class ShopController extends CommonController {
	public function _initialize(){
		parent::_initialize();
	}
    public function add(){
		$Shop = M ("Shop");
		$Type = M ("Type");
		$Shoptype = M ("Shoptype");
		$shop_id = I ('get.shop_id');
		if( $shop_id != ''){
			$res = $Shop->where("shop_id={$shop_id}")->find();
			$this->assign('res',$res);
		}
		$type_ret=$Type->select();
		$this->assign('type_ret',$type_ret);
		$shoptype_ret=$Shoptype->select();
		$this->assign('shoptype_ret',$shoptype_ret);
		$this->display();
    }
	public function addShop(){
		$shop_id = I ('get.shop_id');
		
		$rules = array(
			array('shop_title','require','用户名必须！'), // 用户名必须
			array('seller','require','用户名必须！'), // 用户名必须
			array('present','require','用户名必须！'), // 用户名必须
			array('content','require','用户名必须！'), // 用户名必须
			array('num','require','用户名必须！'), // 用户名必须
		);
		$content=I('post.content', '', '');
		$picture=I('post.picture');
		$kouwei = I ('post.kouwei');
		$Shop = M ("Shop");
		//var_dump($addArticle);	
		$data = $Shop->validate($rules)->create();
		if(!$data){
			$this->error($Shop->getError());
		}else{
				$data['content']=$content;
				$data['addtime']=time();
				$data['kouwei'] = $kouwei;
				/*var_dump($_FILES['picture[]']);
				exit;
				if( $_FILES['picture']!= ''){
					$upload = new \Think\Upload();// 实例化上传类
					$upload->maxSize = 3145728 ;// 设置附件上传大小
					$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
					$path=realpath(__ROOT__);
					$upload->rootPath =  $path.'/Public/uploadfiles/'; // 设置附件上传根目录
					$upload->savePath = ''; // 设置附件上传（子）目录
					// 上传文件 
					$info   =   $upload->upload();
					
					if(!$info) {// 上传错误提示错误信息
						$this->error($upload->getError());
					}else{// 上传成功
						$data['picture']=$picture;
						var_dump($data['picture']);
						exit;
					}			
				}*/
				if( $shop_id == ''){
					$Shop->add($data);
					$this->success("成功！");
				}
				else{		
					$data['picture']=json_encode($picture);
					$Shop->data($data)->where("shop_id={$shop_id}")->save($data);
					$this->success("编辑成功！");
					}
		}
	}
	public function lunbo(){
		$Lunbo = M ('Lunbo');
		$list = $Lunbo->select();
		$Article = M('Article');
		$Type = M ('Type');
		$data = array();
		foreach($list as $val){
			$article = $Article->where("id={$val['article_id']}")->find();
			$type = $Type->where("id={$article['type_id']}")->find();
			$val['type'] = $type['name'];
			$val['title'] = $article['title'];
			$val['user'] = $article['user'];
			$val['picture'] = $article['picture'];
			$data[] = $val;
		}
		$this->assign('data',$data);// 赋值数据集
		$this->display(); 
	}
	 public function alist(){
		$shop_id = I ('get.shop_id'); 
		$act = I ('get.act');
		$Shop = M('Shop'); // 实例化User对象
		$Type = M('Type');
		
		//var_dump($id);
		//exit;
		//$type_id = $Article->where("id={$id}")->select(); 
		//var_dump($type_id);
		//$Type = M('Type'); // 实例化User对象
		//$type_name = $Type->where("id={$type_id}")->getField('name');
		//$this->assign('type_name',$type_name);// 赋值数据集
		/*if($act == 'del'){
			$res = $Shop->where("shop_id={$shop_id}")->delete();
			if($res != false){
				$this->success("删除成功");
			}
			else{
				$this->error($Shop->getError());
			}
			return;		
		}elseif($act == 'lunbo'){
			//先判断是否文章已经存在于轮播列表
			$Lunbo = M ('Lunbo');
			$article_id = $Lunbo->where("article_id={$id}")->find();
			if($article_id !=false ){
				$this->error("该文章已存在于轮播列表！");
			}
			$tid = $Article->where("id={$id}")->find();

			$lunbo_data = array(
				'article_id' => $id,
				'type_id'    =>$tid['type_id'],
				'addtime'    => time()
			);
			$ret = $Lunbo->add($lunbo_data);
			if( $ret != false){
				$this->success("加入轮播成功！");
			}
			else{
				$this->error("加入轮播失败！");
			}
			return;		
		}*/
		$count      = $Shop->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Shop->order('click desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$data = array();
		foreach($list as $val){
			
			$type = $Type->where("type_id={$val['type_id']}")->find();
			$val['type_name'] = $type['type_name'];
			//将json数据转换为数组
			$var = json_decode($val["picture"]);
			$val['headphoto'] = $var[0];
			$data[] = $val;
		}
	
	
		$this->assign('data',$data);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
		
    }

}//
